//  我的-编辑个人资料
const baseUrl='https://gsdse.cn/api/KJRecruit';
Page({
  data: {
    personInfo:{},
    avatarUrl:'', //头像
    canEdit: false,
  },
  
  //确认隐私条款
  showPrim() {
    wx.showModal({
      title: '用户服务协议',
      content: '您的联系方式包括手机与邮箱，仅会用于发送录取通知消息，你的真实姓名我们将给予保密，我们保存此信息仅仅会用于您的招新流程，并向您保证不会另做他用，您是否同意此条款',
      confirmText: "同意",
      cancelText: '不同意'
    }).then(res => {
      if (res.confirm) {
        this.setData({
          canEdit: true
        })
      } else {
        this.setData({
          canEdit: false
        })
      }
    })
  },

  //检测姓名输入是否为空
  inputRealname(name) {
    if (!name) {
      getApp().showMessage('请输入姓名')
      return false
    }else{
      return true
    }
  },
  //检测邮箱输入是否正确
  inputEmail(e) {
    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    if (str.test(e)) {
      return true
    } else {
      getApp().showMessage('请填写正确的邮箱号')
      return false
    }
  },
  //检测手机号输入是否正确
  inputPhoneNumber(e) {
    let str2 = /^1\d{10}$/
    if (str2.test(e)) {
      return true
    } else {
      getApp().showMessage('请填写正确的手机号')
      return false
    }
  },
  //检测班级输入是否为空
  inputClass(schoolClass) {
    if (!schoolClass) {
      getApp().showMessage('请输入班级')
      return false
    }else{
      return true
    }
  },
  //用户上传的新头像将返回的数据设置给this.data.personInfo.avatarUrl
  async inputAvatar(e){
    if(!this.data.canEdit) return
    const avatarUrl = await this.uploadAvatar(e.detail.avatarUrl)
    //获取合规头像地址
    this.setData({
      'personInfo.avatarUrl':avatarUrl
    })
  },
  //保存资料
  async save(e) {
    const that = this
    //获取输入内容
    let newInfo = e.detail.value
    console.log(newInfo)
    //检查是否合规
    const correct = this.inputRealname(newInfo.userName) &&
                    this.inputClass(newInfo.schoolClass) &&
                    this.inputEmail(newInfo.email) &&
                    this.inputPhoneNumber(newInfo.phoneNumber)
    if(!correct){
      getApp().showMessage("请填写完整信息！")
      return;
    }
    if(newInfo.userName.includes("新用户")){
      getApp().showMessage("请删除新用户字眼")
      return
    }
    //合规后修改信息
    //修改内容
    const personInfo = this.data.personInfo
    personInfo.userName = newInfo.userName
    personInfo.email = newInfo.email
    personInfo.schoolClass = newInfo.schoolClass
    personInfo.phoneNumber = newInfo.phoneNumber
    personInfo.nickName=newInfo.nickName
    console.log(personInfo)
    //上传头像
    this.uploadAvatar(personInfo.avatarUrl)
    //调用接口
    if(await getApp().updateUserInfo(this.data.personInfo)){
      //修改更改标记，未修改则再改一次，直到修改完成为止
      that.updateEditStatus();
      //成功后返回上一页
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请检查修改后的姓名是否含"新用户"字眼。若有，请删除对应文字，确保之后查询录取无误！！',
        cancelColor: 'cancelColor',
        success:(res)=>{
          wx.navigateBack()
        }
      })
    }
  },
  uploadAvatar(avatarUrl){
    const that = this
    return new Promise((resolve,reject)=>{
      wx.uploadFile({
        filePath: avatarUrl,
        name: 'file',
        url: getApp().data.domain +  '/uploadAvatar',
        success:function(res){
          const resData = JSON.parse(res.data)
          // console.log(resData.data)
          resolve(resData.data)
        }
      })
    })
  },
  //修改更改标记
  updateEditStatus(){
    getApp().data.infoHaveEdited = true
    wx.setStorageSync('editedInfo', true)
    if(!getApp().data.infoHaveEdited){
      console.log('没改到啊喂！')
      this.updateEditStatus()
    }
  },
  //监听页面显示
  async onShow() {
    var that = this
    //获取用户信息
    this.setData({
      personInfo:getApp().data.personInfo
    })
    console.log(this.data.personInfo)
  },
})